package com.hsy.sunflowerbao.home.store

import com.flux.action.RxAction
import com.flux.action.RxStoreChange
import com.flux.dispatcher.Dispatcher
import com.hsy.sunflowerbao.base.action.ActionType
import com.hsy.sunflowerbao.base.action.Key
import com.hsy.sunflowerbao.base.data.ListBean
import com.hsy.sunflowerbao.base.store.BaseStore
import com.hsy.sunflowerbao.home.data.HomeData

/**
 * Created on 2018/3/28.
 *
 * @author Yang
 */
class ListStore(dispatcher: Dispatcher) : BaseStore(dispatcher) {

    /**
     * 首页数据
     */
    lateinit var homeData: MutableList<HomeData>

    /**
     * 当前页号
     */
    var page = "1"
    private lateinit var tab: String

    override fun onRxAction(action: RxAction) {
        when (action.type) {
            ActionType.GET_HOME_DATA + tab -> {
                val result: ListBean<HomeData> = action[Key.HOME_DATA]
                if (page == "1") homeData = result.data
                else homeData.addAll(result.data)
                page = result.nextPage
            }
            else -> return
        }
        //数据变更，发出对应的Action，通知view刷新
        postChange(RxStoreChange(tag, action.type, action.success))
    }

    fun setRxTag(tab: String) {
        tag += tab
        this.tab = tab
    }
}